Sarue.Template = new Class({
    TEMPLATE: '',
    CALLBACK: null,
    REQUEST: null,
    DATA: null,
    SERVICE: null,
    HANDLER: null,
    REGEX: null,

    construct: function (template) {

        switch (Sarue.Tools.type(arguments[1])) {
            case 'string':
                this.SERVICE = arguments[1];
                this.CALLBACK = arguments[2];
                this.FILTER = arguments[3];
                this.HANDLER = arguments[4];
                this.REGEX = arguments[5];
                break;
            case 'object':
                template = arguments[1].template;
                this.SERVICE = arguments[1].service;
                this.CALLBACK = arguments[1].callback;
                this.FILTER = arguments[1].filter;
                this.HANDLER = arguments[1].handler;
                this.DATA = arguments[1].data;
                this.REGEX = arguments[1].regex;
                break;
            default:
                this.DATA = arguments[1];
                this.CALLBACK = arguments[4];
                this.FILTER = arguments[2];
                this.HANDLER = arguments[3];
                this.REGEX = arguments[5];
                break;
        }

        if (document.getElementById(template)) {
            this.TEMPLATE = [document.getElementById(template)]
        }
        else {
            this.TEMPLATE = $ ? $(template) : null;
        }

        if (this.SERVICE) {
            this.REQUEST = Sarue.XHR({
                url: this.SERVICE,
                onComplete: this.complete
            });
        } else {
            this.complete();
        }
    },

    complete: function () {
        if (this.REQUEST) {
            var response = this.REQUEST.getResponseJSON();

            if (!response.Success) {
                alert(response.Message);
                return false;
            }

            this.DATA = response.Data['Collection'];
        }

        for (var c = 0; c < this.TEMPLATE.length; c++) {
            this.update(this.TEMPLATE[c], this.DATA, this.FILTER, this.HANDLER);
        };

        if (this.CALLBACK)
            this.CALLBACK.apply(this, [response, this.TEMPLATE])
    },

    update: function (wrapper, data, filter, handler) {
        filter = filter || function () { return true };
        handler = handler || function (el) { return el };

        var template = wrapper.originalTemplate || wrapper.innerHTML;

        if (!data)
            return template;

        var length = data.length;

        wrapper.originalTemplate = template;
        wrapper.innerHTML = '';

        for (var count = 0; count < length; count++) {
            var clone = document.createElement('clone');

            if (filter.apply(this, [data[count], count]) != false) {
                clone.innerHTML = Sarue.Tools.srpl(template, data[count], this.REGEX);

                while (clone.firstChild) {
                    var treatament = handler.apply(this, [clone.firstChild, data[count], count]);

                    if (treatament != false) {
                        if (treatament && treatament['nodeName'])
                            wrapper.appendChild(treatament);
                        else if (clone.firstChild)
                            wrapper.appendChild(clone.firstChild);
                    }
                }
            }
        }

        return template;
    }
}, {
    READY: false,

    construct: function () {
        Class.require('Sarue.XHR', this.ready);
    },
    ready: function () {
        this.READY = true;
    }
});